package com.tools.cleanmaster.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.tools.ad.util.StatusBarUtil;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.ui.base.SingleActivity;

/**
 * desc:
 * date: 2018/7/2
 * author: ancun
 */

public class TriggerActivity extends SingleActivity {

    /**
     * 关闭按钮
     **/
    private ImageView mCloseImageView;
    /**
     * 加载动画视图
     **/
    private ImageView mAdAnimationImageView;

    /**
     * 加载动画
     **/
    private AnimatorSet mAnimatorSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trigger);
        StatusBarUtil.setTransparent(this);
        sendEmptyMessage(R.id.msg_ad_trigger_time_out, 8000);
        initView();
    }

    private void initView() {
        mCloseImageView = findViewById(R.id.mCloseImageView);
        mAdAnimationImageView = findViewById(R.id.mAdAnimationImageView);
        startADLoadingAnimation();
    }

    @Override
    public void subHandleMessage(Message msg) {
        super.subHandleMessage(msg);
        if (msg.what == R.id.msg_ad_trigger_loaded) {
            onAdLoaded();
        } else if (msg.what == R.id.msg_ad_trigger_fail) {
            onAdFailed();
        } else if (msg.what == R.id.msg_ad_trigger_time_out) {
            onAdFailed();
        }
    }

    private void onAdLoaded() {
        mAdAnimationImageView.clearAnimation();
        mAnimatorSet.cancel();
        finish();
    }

    private void onAdFailed() {
        mAdAnimationImageView.clearAnimation();
        mAnimatorSet.cancel();
        mAdAnimationImageView.setBackgroundResource(R.drawable.bg_trigger_no_ad);
        mCloseImageView.setVisibility(View.VISIBLE);
        mAdAnimationImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TriggerActivity.this.finish();
            }
        });
        mCloseImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TriggerActivity.this.finish();
            }
        });
    }

    private void startADLoadingAnimation() {
        mAdAnimationImageView.setVisibility(View.VISIBLE);
        mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(ObjectAnimator.ofFloat(mAdAnimationImageView, "rotation", 0, 10, -10, 6, -6, 3, -3, 0));

        for (Animator animator : mAnimatorSet.getChildAnimations()) {
            if (animator instanceof ValueAnimator) {
                ((ValueAnimator) animator).setRepeatCount(-1);
                ((ValueAnimator) animator).setRepeatMode(ValueAnimator.RESTART);
            }
        }
        mAnimatorSet.setDuration(1200);
        mAnimatorSet.start();
    }

}
